//
//  RoImageView.m
//  kyMusicPlayer
//
//  Created by 翁科宇 on 2018/5/15.
//  Copyright © 2018年 翁科宇. All rights reserved.
//

#import "RoImageView.h"

@implementation RoImageView

- (void)startRotating{
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    //旋转一周
    rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
    //旋转时间20秒
    rotateAnimation.duration = 20.0;
    //重复次数,这里用最大次数
    rotateAnimation.repeatCount = MAXFLOAT;
    [self.layer addAnimation:rotateAnimation forKey:nil];
}

- (void)stopRotating{
    CFTimeInterval pausedTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //停止旋转,速度设为0
    self.layer.speed = 0.0;
    //保存时间,恢复旋转需要用到
    self.layer.timeOffset = pausedTime;
    
}


- (void)resumeRotate{
    if (self.layer.timeOffset == 0) {
        [self startRotating];
        return;
    }
    CFTimeInterval pausedTime = self.layer.timeOffset;
    self.layer.speed = 1.0;
    //开始旋转
    self.layer.timeOffset = 0.0;
    self.layer.beginTime = 0.0;
    //恢复时间
    CFTimeInterval timeSincePause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    //从暂停的时间点开始旋转
    self.layer.beginTime = timeSincePause;
    
}
@end
